﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Batman.RefiningPharmacist.Domains;
using Batman.RefiningPharmacist.Services.Filters;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

namespace Batman.RefiningPharmacist.Services
{
    public class CompositeReader : ApplicationService
    {
        public IRepository<Composite> Repository { get; set; }

        /// <summary>
        /// 根据过滤条件查找出数据
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public PagedResultDto<Composite> Get(CompositeFilter filter, PagedResultRequestDto page)
        {
            var query = Repository.GetAll().AsNoTracking().Where(t=> !t.IsDeleted);

            query = query.Where(t => t.CreateBy == filter.UserId);

            var items = query.Skip(page.SkipCount).Take(page.MaxResultCount).ToList();
            var total = query.Count();

            return new PagedResultDto<Composite>(total, items);
        }

        /// <summary>
        /// 获取购物清单项 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public List<Composite> GetCompositeItems(CompositeItemFilter filter)
        {
            var query = Repository.GetAll().AsNoTracking().Where(t=> !t.IsDeleted);

            if (filter.CompositeId.HasValue)
            {
                query = query.Where(t => t.Id == filter.CompositeId.Value);
            }

            query = query.Include(t => t.CompositeItems);

            var results = query.ToList();

            return results;
        }
    }
}
